﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace SG.IPC.VirtualDiskService
{
    public partial class VirtualDiskService : ServiceBase
    {
        private char _driveLetter = 'B';

        public VirtualDiskService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            var common = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            var file = Path.Combine(common, "VirtualDisks", "ipc.img");
            var startInfo = new ProcessStartInfo("imdisk.exe")
                                {
                                    CreateNoWindow = true,
                                    Arguments = string.Format("-a -t vm -o rem -m {1}: -f \"{0}\"", file, _driveLetter)
                                };

            new Process { StartInfo = startInfo }.Start();
        }

        protected override void OnStop()
        {
            var startInfo = new ProcessStartInfo("imdisk.exe")
            {
                CreateNoWindow = true,
                Arguments = string.Format("-D -m {0}:", _driveLetter)
            };

            var process = new Process {StartInfo = startInfo};
            process.Start();
            process.WaitForExit();
        }
    }
}
